package com.fencing_squad.homo.slice;

import com.alibaba.fastjson.JSONObject;
import com.fencing_squad.homo.MainAbility;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.util.AnimationConfig;
import com.fencing_squad.homo.util.CustomComponent;
import com.fencing_squad.homo.util.Data;
import com.fencing_squad.homo.util.Network;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

public class LoginSlice extends AbilitySlice {
    private TextField account;
    private TextField password;
    private Component login;
    private Component loginJumpToCode;
    private Component loginJumpToPsw;
    private TextField telephone;
    private TextField checkCode;
    private Component loginByCode;
    private Button getCheckCode;
    private ComponentContainer loginAll;
    private ComponentContainer loginPassword;
    private ComponentContainer loginCode;
    private String accountValue = "";
    private String passwordValue = "";
    private CommonDialog dialog = null;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        loginAll = (ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_login, null, false);

        super.setUIContent(loginAll);
        loginPassword = (ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_login_password, null, false);
        loginCode = (ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_login_code, null, false);
        loginAll.addComponent(loginPassword);

        initConfig();
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void initConfig() {
        account = (TextField) loginPassword.findComponentById(ResourceTable.Id_account);
        password = (TextField) loginPassword.findComponentById(ResourceTable.Id_password);
        login = loginPassword.findComponentById(ResourceTable.Id_login);
        loginJumpToCode = loginPassword.findComponentById(ResourceTable.Id_checkCode);

        telephone = (TextField) loginCode.findComponentById(ResourceTable.Id_telNumber);
        checkCode = (TextField) loginCode.findComponentById(ResourceTable.Id_checkCode);
        getCheckCode = (Button) loginCode.findComponentById(ResourceTable.Id_getCode);
        loginByCode = loginCode.findComponentById(ResourceTable.Id_login);
        loginJumpToPsw = loginCode.findComponentById(ResourceTable.Id_normal);


        account.addTextObserver(this::inputAccount);
        password.addTextObserver(this::inputPassword);
        telephone.addTextObserver(this::inputTelephone);
        checkCode.addTextObserver(this::inputCode);
        login.setClickedListener(this::login);
        loginByCode.setClickedListener(this::loginByCode);
        getCheckCode.setClickedListener(this::getCode);
        loginJumpToPsw.setClickedListener(this::jumpToPsw);
        loginJumpToCode.setClickedListener(this::jumpToCode);
        getCheckCode.setClickable(false);
        loginByCode.setClickable(false);
        login.setClickable(false);
        AnimationConfig.setClickAnimation(login, getContext(), ResourceTable.Graphic_login_button_clicked, ResourceTable.Graphic_login_button);
        AnimationConfig.setClickAnimation(getCheckCode, getContext(), ResourceTable.Graphic_button_clicked, ResourceTable.Graphic_button);
    }

    private void jumpToPsw(Component component){
        loginAll.removeAllComponents();
        loginAll.addComponent(loginPassword);
    }

    private void jumpToCode(Component component){
        loginAll.removeAllComponents();
        loginAll.addComponent(loginCode);
    }

    private void inputAccount(String text, int start, int before, int count) {
        accountValue = text;
        setLoginActive(!accountValue.equals("") && !passwordValue.equals(""));
    }

    private void inputPassword(String text, int start, int before, int count) {
        passwordValue = text;
        setLoginActive(!accountValue.equals("") && !passwordValue.equals(""));
    }

    private void inputTelephone(String text, int start, int before, int count) {
        accountValue = text;
        setLoginActiveCode(!accountValue.equals(""));
    }

    private void inputCode(String text, int start, int before, int count) {
        passwordValue = text;
        setLoginActiveLoginCode(!accountValue.equals("") && !passwordValue.equals(""));
    }

    private void login(Component component) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("by", "account");
        jsonObject.put("user_id", accountValue);
        jsonObject.put("password", passwordValue);

        Network.postJson("/auth/login", jsonObject.toJSONString(), this::resolve, this::reject);
    }

    private void loginByCode(Component component) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("by", "phone");
        jsonObject.put("phone", accountValue);
        jsonObject.put("input_code", passwordValue);

        Network.postJson("/auth/login", jsonObject.toJSONString(), this::resolve, this::reject);
    }

    private void getCode(Component component) {
        String url = "/auth/check?phone="+accountValue;
        Network.get(url, null, null);
    }

    private void resolve(JSONObject res) {
        Data.Account.isLogin = true;
        Data.Account.token = res.getJSONObject("data").getString("token");
        Network.get("/user", this::validResolve, this::reject);
    }

    private void validResolve(JSONObject res) {
        Data.Account.getFromJSON(res.getJSONObject("data"));
        terminate();
    }

    private void reject(int code, String message) {
        if(code == 404) {
            dialog = CustomComponent.getDialog(getContext(), message, () -> {
                if(dialog != null) {
                    dialog.hide();
                }
            }, () -> {
                if(dialog != null) {
                    dialog.hide();
                }
            });
            dialog.show();
        }
        else {
            System.out.println(message);
        }
    }

    private void setLoginActive(boolean active) {
        if (active) {
            login.setClickable(true);
            AnimationConfig.setBackground(login, getContext(), ResourceTable.Graphic_login_button);
        }
        else {
            login.setClickable(false);
            AnimationConfig.setBackground(login, getContext(), ResourceTable.Graphic_login_button_disabled);
        }
    }

    private void setLoginActiveCode(boolean active) {
        if (active) {
            getCheckCode.setClickable(true);
            AnimationConfig.setBackground(getCheckCode, getContext(), ResourceTable.Graphic_button);
        }
        else {
            getCheckCode.setClickable(false);
            AnimationConfig.setBackground(getCheckCode, getContext(), ResourceTable.Graphic_button_disabled);
        }
    }

    private void setLoginActiveLoginCode(boolean active) {
        if (active) {
            loginByCode.setClickable(true);
            AnimationConfig.setBackground(loginByCode, getContext(), ResourceTable.Graphic_login_button);
        }
        else {
            loginByCode.setClickable(false);
            AnimationConfig.setBackground(loginByCode, getContext(), ResourceTable.Graphic_login_button_disabled);
        }
    }
}
